home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 002 / emacssrc.arc / RANDOM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-05  |  24.4 KB  |  916 lines

  1. /*
  2.  * This file contains the command processing functions for a number of random
  3.  * commands. There is no functional grouping here, for sure.
  4.  */
  5.  
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #include        "edef.h"
  9.  
  10. #if    MEGAMAX & ST520
  11. overlay "random"
  12.  
  13. extern int STncolors;
  14. #endif
  15.  
  16. int     tabsize;                        /* Tab size (0: use real tabs)  */
  17.  
  18. /*
  19.  * Set fill column to n.
  20.  */
  21. setfillcol(f, n)
  22. {
  23.         fillcol = n;
  24.     mlwrite("[Fill column is %d]",n);
  25.         return(TRUE);
  26. }
  27.  
  28. /*
  29.  * Display the current position of the cursor, in origin 1 X-Y coordinates,
  30.  * the character that is under the cursor (in hex), and the fraction of the
  31.  * text that is before the cursor. The displayed column is not the current
  32.  * column, but the column that would be used on an infinite width display.
  33.  * Normally this is bound to "C-X =".
  34.  */
  35. showcpos(f, n)
  36. {
  37.         register LINE   *lp;        /* current line */
  38.         register long   numchars;    /* # of chars in file */
  39.         register int    numlines;    /* # of lines in file */
  40.         register long   predchars;    /* # chars preceding point */
  41.         register int    predlines;    /* # lines preceding point */
  42.         register int    curchar;    /* character under cursor */
  43.         int ratio;
  44.         int col;
  45.     int savepos;            /* temp save for current offset */
  46.     int ecol;            /* column pos/end of current line */
  47.  
  48.     /* starting at the beginning of the buffer */
  49.         lp = lforw(curbp->b_linep);
  50.  
  51.     /* start counting chars and lines */
  52.         numchars = 0;
  53.         numlines = 0;
  54.         while (lp != curbp->b_linep) {
  55.         /* if we are on the current line, record it */
  56.         if (lp == curwp->w_dotp) {
  57.             predlines = numlines;
  58.             predchars = numchars + curwp->w_doto;
  59.             if ((curwp->w_doto) == llength(lp))
  60.                 curchar = '\n';
  61.             else
  62.                 curchar = lgetc(lp, curwp->w_doto);
  63.         }
  64.         /* on to the next line */
  65.         ++numlines;
  66.         numchars += llength(lp) + 1;
  67.         lp = lforw(lp);
  68.         }
  69.  
  70.     /* if at end of file, record it */
  71.     if (curwp->w_dotp == curbp->b_linep) {
  72.         predlines = numlines;
  73.         predchars = numchars;
  74.     }
  75.  
  76.     /* Get real column and end-of-line column. */
  77.     col = getccol(FALSE);
  78.     savepos = curwp->w_doto;
  79.     curwp->w_doto = llength(curwp->w_dotp);
  80.     ecol = getccol(FALSE);
  81.     curwp->w_doto = savepos;
  82.  
  83.         ratio = 0;              /* Ratio before dot. */
  84.         if (numchars != 0)
  85.                 ratio = (100L*predchars) / numchars;
  86.  
  87.     /* summarize and report the info */
  88.     mlwrite("Line %d/%d Col %d/%d Char %D/%D (%d%%) char = 0x%x",
  89.         predlines+1, numlines+1, col, ecol,
  90.         predchars, numchars, ratio, curchar);
  91.         return (TRUE);
  92. }
  93.  
  94. getcline()    /* get the current line number */
  95.  
  96. {
  97.         register LINE   *lp;        /* current line */
  98.         register int    numlines;    /* # of lines before point */
  99.  
  100.     /* starting at the beginning of the buffer */
  101.         lp = lforw(curbp->b_linep);
  102.  
  103.     /* start counting lines */
  104.         numlines = 0;
  105.         while (lp != curbp->b_linep) {
  106.         /* if we are on the current line, record it */
  107.         if (lp == curwp->w_dotp)
  108.             break;
  109.         ++numlines;
  110.         lp = lforw(lp);
  111.         }
  112.  
  113.     /* and return the resulting count */
  114.     return(numlines + 1);
  115. }
  116.  
  117. /*
  118.  * Return current column.  Stop at first non-blank given TRUE argument.
  119.  */
  120. getccol(bflg)
  121. int bflg;
  122. {
  123.         register int c, i, col;
  124.         col = 0;
  125.         for (i=0; i<curwp->w_doto; ++i) {
  126.                 c = lgetc(curwp->w_dotp, i);
  127.                 if (c!=' ' && c!='\t' && bflg)
  128.                         break;
  129.                 if (c == '\t')
  130.                         col |= 0x07;
  131.                 else if (c<0x20 || c==0x7F)
  132.                         ++col;
  133.                 ++col;
  134.         }
  135.         return(col);
  136. }
  137.  
  138. /*
  139.  * Set current column.
  140.  */
  141. setccol(pos)
  142.  
  143. int pos;    /* position to set cursor */
  144.  
  145. {
  146.         register int c;        /* character being scanned */
  147.     register int i;        /* index into current line */
  148.     register int col;    /* current cursor column   */
  149.     register int llen;    /* length of line in bytes */
  150.  
  151.     col = 0;
  152.     llen = llength(curwp->w_dotp);
  153.  
  154.     /* scan the line until we are at or past the target column */
  155.     for (i = 0; i < llen; ++i) {
  156.         /* upon reaching the target, drop out */
  157.         if (col >= pos)
  158.             break;
  159.  
  160.         /* advance one character */
  161.                 c = lgetc(curwp->w_dotp, i);
  162.                 if (c == '\t')
  163.                         col |= 0x07;
  164.                 else if (c<0x20 || c==0x7F)
  165.                         ++col;
  166.                 ++col;
  167.         }
  168.     /* if not long enough... */
  169.     if (col < pos)
  170.         return(FALSE);
  171.  
  172.     /* otherwise...set us at the new position */
  173.     curwp->w_doto = i;
  174.     return(TRUE);
  175. }
  176.  
  177. /*
  178.  * Twiddle the two characters on either side of dot. If dot is at the end of
  179.  * the line twiddle the two characters before it. Return with an error if dot
  180.  * is at the beginning of line; it seems to be a bit pointless to make this
  181.  * work. This fixes up a very common typo with a single stroke. Normally bound
  182.  * to "C-T". This always works within a line, so "WFEDIT" is good enough.
  183.  */
  184. twiddle(f, n)
  185. {
  186.         register LINE   *dotp;
  187.         register int    doto;
  188.         register int    cl;
  189.         register int    cr;
  190.  
  191.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  192.         return(rdonly());    /* we are in read only mode    */
  193.         dotp = curwp->w_dotp;
  194.         doto = curwp->w_doto;
  195.         if (doto==llength(dotp) && --doto<0)
  196.                 return (FALSE);
  197.         cr = lgetc(dotp, doto);
  198.         if (--doto < 0)
  199.                 return (FALSE);
  200.         cl = lgetc(dotp, doto);
  201.         lputc(dotp, doto+0, cr);
  202.         lputc(dotp, doto+1, cl);
  203.         lchange(WFEDIT);
  204.         return (TRUE);
  205. }
  206.  
  207. /*
  208.  * Quote the next character, and insert it into the buffer. All the characters
  209.  * are taken literally, with the exception of the newline, which always has
  210.  * its line splitting meaning. The character is always read, even if it is
  211.  * inserted 0 times, for regularity. Bound to "C-Q"
  212.  */
  213. quote(f, n)
  214. {
  215.         register int    s;
  216.         register int    c;
  217.  
  218.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  219.         return(rdonly());    /* we are in read only mode    */
  220.         c = tgetc();
  221.         if (n < 0)
  222.                 return (FALSE);
  223.         if (n == 0)
  224.                 return (TRUE);
  225.         if (c == '\n') {
  226.                 do {
  227.                         s = lnewline();
  228.                 } while (s==TRUE && --n);
  229.                 return (s);
  230.         }
  231.         return (linsert(n, c));
  232. }
  233.  
  234. /*
  235.  * Set tab size if given non-default argument (n <> 1).  Otherwise, insert a
  236.  * tab into file.  If given argument, n, of zero, change to true tabs.
  237.  * If n > 1, simulate tab stop every n-characters using spaces. This has to be
  238.  * done in this slightly funny way because the tab (in ASCII) has been turned
  239.  * into "C-I" (in 10 bit code) already. Bound to "C-I".
  240.  */
  241. tab(f, n)
  242. {
  243.         if (n < 0)
  244.                 return (FALSE);
  245.         if (n == 0 || n > 1) {
  246.                 tabsize = n;
  247.                 return(TRUE);
  248.         }
  249.         if (! tabsize)
  250.                 return(linsert(1, '\t'));
  251.         return(linsert(tabsize - (getccol(FALSE) % tabsize), ' '));
  252. }
  253.  
  254. /*
  255.  * Open up some blank space. The basic plan is to insert a bunch of newlines,
  256.  * and then back up over them. Everything is done by the subcommand
  257.  * procerssors. They even handle the looping. Normally this is bound to "C-O".
  258.  */
  259. openline(f, n)
  260. {
  261.         register int    i;
  262.         register int    s;
  263.  
  264.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  265.         return(rdonly());    /* we are in read only mode    */
  266.         if (n < 0)
  267.                 return (FALSE);
  268.         if (n == 0)
  269.                 return (TRUE);
  270.         i = n;                                  /* Insert newlines.     */
  271.         do {
  272.                 s = lnewline();
  273.         } while (s==TRUE && --i);
  274.         if (s == TRUE)                          /* Then back up overtop */
  275.                 s = backchar(f, n);             /* of them all.         */
  276.         return (s);
  277. }
  278.  
  279. /*
  280.  * Insert a newline. Bound to "C-M". If we are in CMODE, do automatic
  281.  * indentation as specified.
  282.  */
  283. newline(f, n)
  284. {
  285.     register int    s;
  286.  
  287.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  288.         return(rdonly());    /* we are in read only mode    */
  289.     if (n < 0)
  290.         return (FALSE);
  291.  
  292.     /* if we are in C mode and this is a default <NL> */
  293.     if (n == 1 && (curbp->b_mode & MDCMOD) &&
  294.         curwp->w_dotp != curbp->b_linep)
  295.         return(cinsert());
  296.  
  297.         /*
  298.          * If a newline was typed, fill column is defined, the argument is non-
  299.          * negative, wrap mode is enabled, and we are now past fill column,
  300.      * and we are not read-only, perform word wrap.
  301.          */
  302.         if ((curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
  303.         getccol(FALSE) > fillcol &&
  304.         (curwp->w_bufp->b_mode & MDVIEW) == FALSE)
  305.         execute(META|SPEC|'W', FALSE, 1);
  306.  
  307.     /* insert some lines */
  308.     while (n--) {
  309.         if ((s=lnewline()) != TRUE)
  310.             return (s);
  311.     }
  312.     return (TRUE);
  313. }
  314.  
  315. cinsert()    /* insert a newline and indentation for C */
  316.  
  317. {
  318.     register char *cptr;    /* string pointer into text to copy */
  319.     register int tptr;    /* index to scan into line */
  320.     register int bracef;    /* was there a brace at the end of line? */
  321.     register int i;
  322.     char ichar[NSTRING];    /* buffer to hold indent of last line */
  323.  
  324.     /* grab a pointer to text to copy indentation from */
  325.     cptr = &curwp->w_dotp->l_text[0];
  326.  
  327.     /* check for a brace */
  328.     tptr = curwp->w_doto - 1;
  329.     bracef = (cptr[tptr] == '{');
  330.  
  331.     /* save the indent of the previous line */
  332.     i = 0;
  333.     while ((i < tptr) && (cptr[i] == ' ' || cptr[i] == '\t')
  334.         && (i < NSTRING - 1)) {
  335.         ichar[i] = cptr[i];
  336.         ++i;
  337.     }
  338.     ichar[i] = 0;        /* terminate it */
  339.  
  340.     /* put in the newline */
  341.     if (lnewline() == FALSE)
  342.         return(FALSE);
  343.  
  344.     /* and the saved indentation */
  345.     i = 0;
  346.     while (ichar[i])
  347.         linsert(1, ichar[i++]);
  348.  
  349.     /* and one more tab for a brace */
  350.     if (bracef)
  351.         tab(FALSE, 1);
  352.  
  353.     return(TRUE);
  354. }
  355.  
  356. insbrace(n, c)    /* insert a brace into the text here...we are in CMODE */
  357.  
  358. int n;    /* repeat count */
  359. int c;    /* brace to insert (always { for now) */
  360.  
  361. {
  362.     register int ch;    /* last character before input */
  363.     register int i;
  364.     register int target;    /* column brace should go after */
  365.  
  366.     /* if we are at the beginning of the line, no go */
  367.     if (curwp->w_doto == 0)
  368.         return(linsert(n,c));
  369.         
  370.     /* scan to see if all space before this is white space */
  371.     for (i = curwp->w_doto - 1; i >= 0; --i) {
  372.         ch = lgetc(curwp->w_dotp, i);
  373.         if (ch != ' ' && ch != '\t')
  374.             return(linsert(n, c));
  375.     }
  376.  
  377.     /* delete back first */
  378.     target = getccol(FALSE);    /* calc where we will delete to */
  379.     target -= 1;
  380.     target -= target % (tabsize == 0 ? 8 : tabsize);
  381.     while (getccol(FALSE) > target)
  382.         backdel(FALSE, 1);
  383.  
  384.     /* and insert the required brace(s) */
  385.     return(linsert(n, c));
  386. }
  387.  
  388. inspound()    /* insert a # into the text here...we are in CMODE */
  389.  
  390. {
  391.     register int ch;    /* last character before input */
  392.     register int i;
  393.  
  394.     /* if we are at the beginning of the line, no go */
  395.     if (curwp->w_doto == 0)
  396.         return(linsert(1,'#'));
  397.         
  398.     /* scan to see if all space before this is white space */
  399.     for (i = curwp->w_doto - 1; i >= 0; --i) {
  400.         ch = lgetc(curwp->w_dotp, i);
  401.         if (ch != ' ' && ch != '\t')
  402.             return(linsert(1, '#'));
  403.     }
  404.  
  405.     /* delete back first */
  406.     while (getccol(FALSE) >= 1)
  407.         backdel(FALSE, 1);
  408.  
  409.     /* and insert the required pound */
  410.     return(linsert(1, '#'));
  411. }
  412.  
  413. /*
  414.  * Delete blank lines around dot. What this command does depends if dot is
  415.  * sitting on a blank line. If dot is sitting on a blank line, this command
  416.  * deletes all the blank lines above and below the current line. If it is
  417.  * sitting on a non blank line then it deletes all of the blank lines after
  418.  * the line. Normally this command is bound to "C-X C-O". Any argument is
  419.  * ignored.
  420.  */
  421. deblank(f, n)
  422. {
  423.         register LINE   *lp1;
  424.         register LINE   *lp2;
  425.         long nld;
  426.  
  427.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  428.         return(rdonly());    /* we are in read only mode    */
  429.         lp1 = curwp->w_dotp;
  430.         while (llength(lp1)==0 && (lp2=lback(lp1))!=curbp->b_linep)
  431.                 lp1 = lp2;
  432.         lp2 = lp1;
  433.         nld = 0;
  434.         while ((lp2=lforw(lp2))!=curbp->b_linep && llength(lp2)==0)
  435.                 ++nld;
  436.         if (nld == 0)
  437.                 return (TRUE);
  438.         curwp->w_dotp = lforw(lp1);
  439.         curwp->w_doto = 0;
  440.         return (ldelete(nld, FALSE));
  441. }
  442.  
  443. /*
  444.  * Insert a newline, then enough tabs and spaces to duplicate the indentation
  445.  * of the previous line. Assumes tabs are every eight characters. Quite simple.
  446.  * Figure out the indentation of the current line. Insert a newline by calling
  447.  * the standard routine. Insert the indentation by inserting the right number
  448.  * of tabs and spaces. Return TRUE if all ok. Return FALSE if one of the
  449.  * subcomands failed. Normally bound to "C-J".
  450.  */
  451. indent(f, n)
  452. {
  453.         register int    nicol;
  454.         register int    c;
  455.         register int    i;
  456.  
  457.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  458.         return(rdonly());    /* we are in read only mode    */
  459.         if (n < 0)
  460.                 return (FALSE);
  461.         while (n--) {
  462.                 nicol = 0;
  463.                 for (i=0; i<llength(curwp->w_dotp); ++i) {
  464.                         c = lgetc(curwp->w_dotp, i);
  465.                         if (c!=' ' && c!='\t')
  466.                                 break;
  467.                         if (c == '\t')
  468.                                 nicol |= 0x07;
  469.                         ++nicol;
  470.                 }
  471.                 if (lnewline() == FALSE
  472.                 || ((i=nicol/8)!=0 && linsert(i, '\t')==FALSE)
  473.                 || ((i=nicol%8)!=0 && linsert(i,  ' ')==FALSE))
  474.                         return (FALSE);
  475.         }
  476.         return (TRUE);
  477. }
  478.  
  479. /*
  480.  * Delete forward. This is real easy, because the basic delete routine does
  481.  * all of the work. Watches for negative arguments, and does the right thing.
  482.  * If any argument is present, it kills rather than deletes, to prevent loss
  483.  * of text if typed with a big argument. Normally bound to "C-D".
  484.  */
  485. forwdel(f, n)
  486. {
  487.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  488.         return(rdonly());    /* we are in read only mode    */
  489.         if (n < 0)
  490.                 return (backdel(f, -n));
  491.         if (f != FALSE) {                       /* Really a kill.       */
  492.                 if ((lastflag&CFKILL) == 0)
  493.                         kdelete();
  494.                 thisflag |= CFKILL;
  495.         }
  496.         return (ldelete((long)n, f));
  497. }
  498.  
  499. /*
  500.  * Delete backwards. This is quite easy too, because it's all done with other
  501.  * functions. Just move the cursor back, and delete forwards. Like delete
  502.  * forward, this actually does a kill if presented with an argument. Bound to
  503.  * both "RUBOUT" and "C-H".
  504.  */
  505. backdel(f, n)
  506. {
  507.         register int    s;
  508.  
  509.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  510.         return(rdonly());    /* we are in read only mode    */
  511.         if (n < 0)
  512.                 return (forwdel(f, -n));
  513.         if (f != FALSE) {                       /* Really a kill.       */
  514.                 if ((lastflag&CFKILL) == 0)
  515.                         kdelete();
  516.                 thisflag |= CFKILL;
  517.         }
  518.         if ((s=backchar(f, n)) == TRUE)
  519.                 s = ldelete((long)n, f);
  520.         return (s);
  521. }
  522.  
  523. /*
  524.  * Kill text. If called without an argument, it kills from dot to the end of
  525.  * the line, unless it is at the end of the line, when it kills the newline.
  526.  * If called with an argument of 0, it kills from the start of the line to dot.
  527.  * If called with a positive argument, it kills from dot forward over that
  528.  * number of newlines. If called with a negative argument it kills backwards
  529.  * that number of newlines. Normally bound to "C-K".
  530.  */
  531. killtext(f, n)
  532. {
  533.         register LINE   *nextp;
  534.         long chunk;
  535.  
  536.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  537.         return(rdonly());    /* we are in read only mode    */
  538.         if ((lastflag&CFKILL) == 0)             /* Clear kill buffer if */
  539.                 kdelete();                      /* last wasn't a kill.  */
  540.         thisflag |= CFKILL;
  541.         if (f == FALSE) {
  542.                 chunk = llength(curwp->w_dotp)-curwp->w_doto;
  543.                 if (chunk == 0)
  544.                         chunk = 1;
  545.         } else if (n == 0) {
  546.                 chunk = curwp->w_doto;
  547.                 curwp->w_doto = 0;
  548.         } else if (n > 0) {
  549.                 chunk = llength(curwp->w_dotp)-curwp->w_doto+1;
  550.                 nextp = lforw(curwp->w_dotp);
  551.                 while (--n) {
  552.                         if (nextp == curbp->b_linep)
  553.                                 return (FALSE);
  554.                         chunk += llength(nextp)+1;
  555.                         nextp = lforw(nextp);
  556.                 }
  557.         } else {
  558.                 mlwrite("neg kill");
  559.                 return (FALSE);
  560.         }
  561.         return(ldelete(chunk, TRUE));
  562. }
  563.  
  564. setmode(f, n)    /* prompt and set an editor mode */
  565.  
  566. int f, n;    /* default and argument */
  567.  
  568. {
  569.     adjustmode(TRUE, FALSE);
  570. }
  571.  
  572. delmode(f, n)    /* prompt and delete an editor mode */
  573.  
  574. int f, n;    /* default and argument */
  575.  
  576. {
  577.     adjustmode(FALSE, FALSE);
  578. }
  579.  
  580. setgmode(f, n)    /* prompt and set a global editor mode */
  581.  
  582. int f, n;    /* default and argument */
  583.  
  584. {
  585.     adjustmode(TRUE, TRUE);
  586. }
  587.  
  588. delgmode(f, n)    /* prompt and delete a global editor mode */
  589.  
  590. int f, n;    /* default and argument */
  591.  
  592. {
  593.     adjustmode(FALSE, TRUE);
  594. }
  595.  
  596. adjustmode(kind, global)    /* change the editor mode status */
  597.  
  598. int kind;    /* true = set,        false = delete */
  599. int global;    /* true = global flag,    false = current buffer flag */
  600. {
  601.     register char *scan;        /* scanning pointer to convert prompt */
  602.     register int i;            /* loop index */
  603.     register status;        /* error return on input */
  604. #if    COLOR
  605.     register int uflag;        /* was modename uppercase?    */
  606. #endif
  607.     char prompt[50];    /* string to prompt user with */
  608.     char cbuf[NPAT];        /* buffer to recieve mode name into */
  609.  
  610.     /* build the proper prompt string */
  611.     if (global)
  612.         strcpy(prompt,"Global mode to ");
  613.     else
  614.         strcpy(prompt,"Mode to ");
  615.  
  616.     if (kind == TRUE)
  617.         strcat(prompt, "add: ");
  618.     else
  619.         strcat(prompt, "delete: ");
  620.  
  621.     /* prompt the user and get an answer */
  622.  
  623.     status = mlreply(prompt, cbuf, NPAT - 1);
  624.     if (status != TRUE)
  625.         return(status);
  626.  
  627.     /* make it uppercase */
  628.  
  629.     scan = cbuf;
  630. #if    COLOR
  631.     uflag = (*scan >= 'A' && *scan <= 'Z');
  632. #endif
  633.     while (*scan != 0) {
  634.         if (*scan >= 'a' && *scan <= 'z')
  635.             *scan = *scan - 32;
  636.         scan++;
  637.     }
  638.  
  639.     /* test it first against the colors we know */
  640.     for (i=0; i<NCOLORS; i++) {
  641.         if (strcmp(cbuf, cname[i]) == 0) {
  642.             /* finding the match, we set the color */
  643. #if    COLOR
  644.             if (uflag)
  645.                 if (global)
  646.                     gfcolor = i;
  647.                 else
  648.                     curwp->w_fcolor = i;
  649.             else
  650.                 if (global)
  651.                     gbcolor = i;
  652.                 else
  653.                     curwp->w_bcolor = i;
  654.  
  655.             curwp->w_flag |= WFCOLR;
  656. #endif
  657.             mlerase();
  658.             return(TRUE);
  659.         }
  660.     }
  661.  
  662.     /* test it against the modes we know */
  663.  
  664.     for (i=0; i < NUMMODES; i++) {
  665.         if (strcmp(cbuf, modename[i]) == 0) {
  666.             /* finding a match, we process it */
  667.             if (kind == TRUE)
  668.                 if (global)
  669.                     gmode |= (1 << i);
  670.                 else
  671.                     curwp->w_bufp->b_mode |= (1 << i);
  672.             else
  673.                 if (global)
  674.                     gmode &= ~(1 << i);
  675.                 else
  676.                     curwp->w_bufp->b_mode &= ~(1 << i);
  677.             /* display new mode line */
  678.             if (global == 0)
  679.                 upmode();
  680.             mlerase();    /* erase the junk */
  681.             return(TRUE);
  682.         }
  683.     }
  684.  
  685.     mlwrite("No such mode!");
  686.     return(FALSE);
  687. }
  688.  
  689. /*    This function simply clears the message line,
  690.         mainly for macro usage            */
  691.  
  692. clrmes(f, n)
  693.  
  694. int f, n;    /* arguments ignored */
  695.  
  696. {
  697.     mlwrite("");
  698.     return(TRUE);
  699. }
  700.  
  701. /*    This function writes a string on the message line
  702.         mainly for macro usage            */
  703.  
  704. writemsg(f, n)
  705.  
  706. int f, n;    /* arguments ignored */
  707.  
  708. {
  709.     register char *sp;    /* pointer into buf to expand %s */
  710.     register char *np;    /* ptr into nbuf */
  711.     register int status;
  712.     char buf[NPAT];        /* buffer to recieve message into */
  713.     char nbuf[NPAT*2];    /* buffer to expand string into */
  714.  
  715.     if ((status = mlreply("Message to write: ", buf, NPAT - 1)) != TRUE)
  716.         return(status);
  717.  
  718.     /* expand all '%' to "%%" so mlwrite won't expect arguments */
  719.     sp = buf;
  720.     np = nbuf;
  721.     while (*sp) {
  722.         *np++ = *sp;
  723.         if (*sp++ == '%')
  724.             *np++ = '%';
  725.     }
  726.     *np = '\0';
  727.     mlwrite(nbuf);
  728.     return(TRUE);
  729. }
  730.  
  731. #if    CFENCE
  732. /*    the cursor is moved to a matching fence    */
  733.  
  734. getfence(f, n)
  735.  
  736. int f, n;    /* not used */
  737.  
  738. {
  739.     register LINE *oldlp;    /* original line pointer */
  740.     register int oldoff;    /* and offset */
  741.     register int sdir;    /* direction of search (1/-1) */
  742.     register int count;    /* current fence level count */
  743.     register char ch;    /* fence type to match against */
  744.     register char ofence;    /* open fence */
  745.     register char c;    /* current character in scan */
  746.  
  747.     /* save the original cursor position */
  748.     oldlp = curwp->w_dotp;
  749.     oldoff = curwp->w_doto;
  750.  
  751.     /* get the current character */
  752.     if (oldoff == llength(oldlp))
  753.         ch = '\n';
  754.     else
  755.         ch = lgetc(oldlp, oldoff);
  756.  
  757.     /* setup proper matching fence */
  758.     switch (ch) {
  759.         case '(': ofence = ')'; sdir = FORWARD; break;
  760.         case '{': ofence = '}'; sdir = FORWARD; break;
  761.         case '[': ofence = ']'; sdir = FORWARD; break;
  762.         case ')': ofence = '('; sdir = REVERSE; break;
  763.         case '}': ofence = '{'; sdir = REVERSE; break;
  764.         case ']': ofence = '['; sdir = REVERSE; break;
  765.         default: TTbeep(); return(FALSE);
  766.     }
  767.  
  768.     /* set up for scan */
  769.     count = 1;
  770.     if (sdir == REVERSE)
  771.         backchar(FALSE, 1);
  772.     else
  773.         forwchar(FALSE, 1);
  774.  
  775.     /* scan until we find it, or reach the end of file */
  776.     while (count > 0) {
  777.         if (curwp->w_doto == llength(curwp->w_dotp))
  778.             c = '\n';
  779.         else
  780.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  781.         if (c == ch)
  782.             ++count;
  783.         if (c == ofence)
  784.             --count;
  785.         if (sdir == FORWARD)
  786.             forwchar(FALSE, 1);
  787.         else
  788.             backchar(FALSE, 1);
  789.         if (boundry(curwp->w_dotp, curwp->w_doto, sdir))
  790.             break;
  791.     }
  792.  
  793.     /* if count is zero, we have a match, move the sucker */
  794.     if (count == 0) {
  795.         if (sdir == FORWARD)
  796.             backchar(FALSE, 1);
  797.         else
  798.             forwchar(FALSE, 1);
  799.         curwp->w_flag |= WFMOVE;
  800.         return(TRUE);
  801.     }
  802.  
  803.     /* restore the current position */
  804.     curwp->w_dotp = oldlp;
  805.     curwp->w_doto = oldoff;
  806.     TTbeep();
  807.     return(FALSE);
  808. }
  809. #endif
  810.  
  811. /*    Close fences are matched against their partners, and if
  812.     on screen the cursor briefly lights there        */
  813.  
  814. fmatch(ch)
  815.  
  816. char ch;    /* fence type to match against */
  817.  
  818. {
  819.     register LINE *oldlp;    /* original line pointer */
  820.     register int oldoff;    /* and offset */
  821.     register LINE *toplp;    /* top line in current window */
  822.     register int count;    /* current fence level count */
  823.     register char opench;    /* open fence */
  824.     register char c;    /* current character in scan */
  825.     register int i;
  826.  
  827.     /* first get the display update out there */
  828.     update(FALSE);
  829.  
  830.     /* save the original cursor position */
  831.     oldlp = curwp->w_dotp;
  832.     oldoff = curwp->w_doto;
  833.  
  834.     /* setup proper open fence for passed close fence */
  835.     if (ch == ')')
  836.         opench = '(';
  837.     else if (ch == '}')
  838.         opench = '{';
  839.     else
  840.         opench = '[';
  841.  
  842.     /* find the top line and set up for scan */
  843.     toplp = curwp->w_linep->l_bp;
  844.     count = 1;
  845.     backchar(FALSE, 2);
  846.  
  847.     /* scan back until we find it, or reach past the top of the window */
  848.     while (count > 0 && curwp->w_dotp != toplp) {
  849.         if (curwp->w_doto == llength(curwp->w_dotp))
  850.             c = '\n';
  851.         else
  852.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  853.         if (c == ch)
  854.             ++count;
  855.         if (c == opench)
  856.             --count;
  857.         backchar(FALSE, 1);
  858.         if (curwp->w_dotp == curwp->w_bufp->b_linep->l_fp &&
  859.             curwp->w_doto == 0)
  860.             break;
  861.     }
  862.  
  863.     /* if count is zero, we have a match, display the sucker */
  864.     /* there is a real machine dependant timing problem here we have
  865.        yet to solve......... */
  866.     if (count == 0) {
  867.         forwchar(FALSE, 1);
  868.         for (i = 0; i < term.t_pause; i++)
  869.             update(FALSE);
  870.     }
  871.  
  872.     /* restore the current position */
  873.     curwp->w_dotp = oldlp;
  874.     curwp->w_doto = oldoff;
  875.     return(TRUE);
  876. }
  877.  
  878. istring(f, n)    /* ask for and insert a string into the current
  879.            buffer at the current point */
  880.  
  881. int f, n;    /* ignored arguments */
  882.  
  883. {
  884.     register char *tp;    /* pointer into string to add */
  885.     register int status;    /* status return code */
  886.     char tstring[NPAT+1];    /* string to add */
  887.  
  888.     /* ask for string to insert */
  889.     status = mlreplyt("String to insert<META>: ", tstring, NPAT, metac);
  890.     if (status != TRUE)
  891.         return(status);
  892.  
  893.     if (f == FALSE)
  894.         n = 1;
  895.  
  896.     if (n < 0)
  897.         n = - n;
  898.  
  899.     /* insert it */
  900.     while (n--) {
  901.         tp = &tstring[0];
  902.         while (*tp) {
  903.             if (*tp == 0x0a)
  904.                 status = lnewline();
  905.             else
  906.                 status = linsert(1, *tp);
  907.             ++tp;
  908.             if (status != TRUE)
  909.                 return(status);
  910.         }
  911.     }
  912.         
  913.     return(TRUE);
  914. }
  915.  
  916.